package com.users.rn.rncommon.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.users.rn.common.download.RNPluginDownloadManager;
import com.users.rn.rncommon.R;
import com.users.rn.rncommon.download.PluginDownloadProgressListener;
import com.users.rn.rncommon.router.RNSchemeExecute;
import com.users.rn.rncommon.util.RNLogger;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 15:14
 */
public class RNPluginUpgradeActivity extends AppCompatActivity implements View.OnClickListener {
    private String actionScheme = "";
    private String mPluginId = "";
    private ProgressBar progressBar;
    private TextView tv_progressdes;
    private TextView tv_sizepercent;
    private ImageView iv_close;
    private PluginDownloadProgressListener listener = null;

    public static void start(Context context, String pluginId, String next_action_scheme, String plugin_type) {
        if (context == null || TextUtils.isEmpty(pluginId)) {
            return;
        }
        try {
            Intent intent = new Intent(context, RNPluginUpgradeActivity.class);
            intent.putExtra(EXTRA_PLUGIN_ID, pluginId);
            intent.putExtra(EXTRA_SCHEME_KEY, next_action_scheme);
            intent.putExtra(EXTRA_PLUGIN_TYPE, plugin_type);
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                activity.startActivity(intent);
            } else {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rn_plugin_upgrade_layout);
        initView();
        initData();
        initIntent();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        RNLogger.i("RnPluginProgressActivity onNewIntent");
        initIntent();
    }

    @Override
    protected void onResume() {
        super.onResume();
        RNPluginDownloadManager.INSTANCE.connect(getApplicationContext(), mPluginId, getListener());
    }

    private PluginDownloadProgressListener getListener() {
        if (listener == null) {
            listener = new ReactNativePluginDownloadListener();
        }
        return listener;
    }


    private void initView() {
        tv_progressdes = (TextView) findViewById(R.id.tv_progressdes);
        tv_sizepercent = (TextView) findViewById(R.id.tv_sizepercent);
//        tv_updatetips = (TextView) findViewById(R.id.tv_updatetips);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        iv_close = (ImageView) findViewById(R.id.iv_close);
        iv_close.setOnClickListener(this);

        progressBar.setProgress(50);
    }

    private void initIntent() {
        if (getIntent() != null) {
            actionScheme = getIntent().getStringExtra(EXTRA_SCHEME_KEY);
            mPluginId = getIntent().getStringExtra(EXTRA_PLUGIN_ID);
//            plugintType = getIntent().getStringExtra(EXTRA_PLUGIN_TYPE);
        }
    }

    private void initData() {
        if (listener == null) {
            listener = new ReactNativePluginDownloadListener();
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == iv_close.getId()) {
            finish();
        }
    }


    public class ReactNativePluginDownloadListener implements PluginDownloadProgressListener {

        @Override
        public void onSuccess(String pluginId) {
            RNLogger.i("RnPluginProgressActivity RnPluginListener onSuccess pluginId:" + pluginId);
            RNPluginUpgradeActivity.this.runOnUiThread(() -> {
                if (mPluginId.equals(pluginId)) {
                    progressBar.setProgress(100);
                    tv_progressdes.setText(String.format("%d%%", 100));
                    RNSchemeExecute.getInstance().openPage(RNPluginUpgradeActivity.this, actionScheme);
                    finish();
                }
            });
        }

        @Override
        public void onFail(String pluginId, int status) {
            RNLogger.i("RnPluginProgressActivity RnPluginListener onFail pluginId:" + pluginId + ",status:" + status);
            RNPluginUpgradeActivity.this.runOnUiThread(() -> {
                if (mPluginId.equals(pluginId)) {
                    Toast.makeText(RNPluginUpgradeActivity.this, "下载插件失败", Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
        public void onDownloading(String pluginId, int progress) {
            RNPluginUpgradeActivity.this.runOnUiThread(() -> {
                if (mPluginId.equals(pluginId)) {
                    progressBar.setProgress(progress);
                    tv_progressdes.setText(String.format("%d%%", progress));
                }
            });
        }
    }

    private static final String EXTRA_SCHEME_KEY = "next_action_scheme";
    private static final String EXTRA_PLUGIN_ID = "plugin_id";
    private static final String EXTRA_PLUGIN_TYPE = "plugin_type";
}
